<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\blog\api\model;

use think\Model;
use app\api\model\User;

class BlogComment extends Model
{
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    // 关联模型
    public function blog()
    {
        return $this->hasOne(Blog::class, 'id', 'blog_id')->append(['url','comment_count','collect_count']);;
    }
    
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->append(['url', 'group']);
    }

    public function replyUser()
    {
        return $this->hasOne(User::class, 'id', 'reply_user_id');
    }

    // 搜索器
    public function searchDateAttr($query, $value, $array)
    {
        if (! empty($value)) { 
            $query->whereBetweenTime('create_time', $value[0], $value[1]);
        }
    }

    public function searchKeywordAttr($query, $value, $array)
    {
        $query->where('content|blog_title', 'like', '%' . $value . '%');
    }
    
    // 获取器
    public function getFriendTimeAttr($value, $array)
    {
        return friend_time($array['create_time']);
    }
    
    public function getLoadingAttr($value, $array)
    {
        return false;
    }
    
    public function getLikeAttr($value, $array)
    {
        return BlogCommentLike::where('comment_id', $array['id'])->count();
    }
    
    public function getIsLikeAttr($value, $array)
    {
        if (BlogCommentLike::where('comment_id', $array['id'])->where('user_id', input('user_id'))->value('id')) {
            return true;
        } else {
            return false;
        }
    }
}